package com.wolf.controller;

import com.wolf.pojo.Wl_commodity;
import com.wolf.service.Wl_commodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ComponentScan
public class Wl_commodityController {

    @Autowired
    Wl_commodityService wl_commodityService;

    /**
     *查询所有
     * @return
     */
    @RequestMapping("wl_commodity_get")
    public String get(ModelMap modelMap){
        //查询产品是否可以代理下拉框
        List<Wl_commodity> getByIsProxy_list = wl_commodityService.getByIsProxy();
        modelMap.addAttribute("wl_commodity_getByIsProxy_list",getByIsProxy_list);

        //查询产品渠道下拉框
        List<Wl_commodity> getByChannel_list = wl_commodityService.getByChannel();
        modelMap.addAttribute("wl_commodity_getByChannel_list",getByChannel_list);

        //查询产品状态下拉框
        List<Wl_commodity> getByStatus_list = wl_commodityService.getByStatus();
        modelMap.addAttribute("wl_commodity_getByStatus_list",getByStatus_list);

        //查询所有的list
        List<Wl_commodity> list = wl_commodityService.getAll();
        modelMap.addAttribute("wl_commodity_getList",list);
        return "table_Wl_commodity";
    }

    /**
     * 模糊查询
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_commodity_fuzzyQuery")
    public String fuzzyQuery(ModelMap modelMap,Integer status,Integer channel,Integer isproxy,String commodityname,String catename){
        //查询产品是否可以代理下拉框
        List<Wl_commodity> getByIsProxy_list = wl_commodityService.getByIsProxy();
        modelMap.addAttribute("wl_commodity_getByIsProxy_list",getByIsProxy_list);

        //查询产品渠道下拉框
        List<Wl_commodity> getByChannel_list = wl_commodityService.getByChannel();
        modelMap.addAttribute("wl_commodity_getByChannel_list",getByChannel_list);

        //查询产品状态下拉框
        List<Wl_commodity> getByStatus_list = wl_commodityService.getByStatus();
        modelMap.addAttribute("wl_commodity_getByStatus_list",getByStatus_list);

        //得到结果返回前台
        List<Wl_commodity> list = wl_commodityService.fuzzyQuery(status,channel,isproxy,commodityname,catename);
        modelMap.addAttribute("wl_commodity_getList",list);
        return "table_Wl_commodity";
    }

    /**
     * 修改上/下架
     */
    @RequestMapping("statusUpdata")
    public String statusUpdata(String id,Integer status){
        if(status==0){
            status=1;
        }else {
            status=0;
        }
        wl_commodityService.statusUpdata(id,status);
        return "redirect:wl_commodity_get";
    }

    /**
     * 禁用/启用
     */
    @RequestMapping("isproxyUpdata")
    public String isproxyUpdata(String id,Integer isproxy){
        if(isproxy==0){
            isproxy=1;
        }else {
            isproxy=0;
        }
        wl_commodityService.isproxyUpdata(id,isproxy);
        return "redirect:wl_commodity_get";
    }

    /**
     * 删除
     */
    @RequestMapping("wl_commodity_remove")
    public String deleteByPrimaryKey(String id){
        wl_commodityService.deleteByPrimaryKey(id);
        return "redirect:wl_commodity_get";
    }

    /**
     * 新增
     */
    @RequestMapping("wl_commodity_Insert")
    public String wl_commodity_Insert(Wl_commodity wl_commodity, String[] shux, String[] shuxz, HttpServletRequest request,String[] channelcheck){
        //设置主键的值 值为当前日期
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String now_date = df.format(new Date());
        wl_commodity.setId(now_date);
        String [] image = request.getParameterValues("image");
        //设置变量，用来得到指定格式的值
        String property="";
        for (int i=0;i<shux.length;i++){
            if(property.equals("")){
                property =property+ '"' + shux[i] + '"' + ":[\"" + shuxz[i] + "\"]";
            }else if(!shux[i].equals("") && shux[i]!=null){
                property =property+ ","+'"' + shux[i] + '"' + ":[\"" + shuxz[i] + "\"]";
            }
        }
        String newproperty = "{"+property+"}";
        wl_commodity.setProperty(newproperty);
        if(image!=null){
            String images="";
            for(int i=0;i<image.length;i++){
                //img是我自己在项目中添加的一个文件夹
                images+="img/"+image[i]+",/";
            }
            images=images.substring(0,images.lastIndexOf(",/"));
            wl_commodity.setImages(images);
        }

        //遍历渠道存入
        String newchannelcheck="";
        for(int i=0;i<channelcheck.length;i++){
            newchannelcheck = newchannelcheck+channelcheck[i];
        }
        wl_commodity.setChannel(newchannelcheck);

        int result = wl_commodityService.insertSelective(wl_commodity);
        if(result>0){
            return "redirect:wl_commodity_get";
        }else{
            return "500";
        }
    }



    /**
     * 修改
     */
    String jsonData="";
    @RequestMapping("wl_commodity_ById")
    public String wl_commodity_ById(HttpServletRequest request,String id){
        Wl_commodity wl = wl_commodityService.selectByPrimaryKey(id);
        jsonData=wl.getProperty();

        //将images属性的值，根据，/进行分割，再把这个数组保存起来
        String [] subimgs = wl.getImages().split(",/");
        request.setAttribute("subimgs",subimgs);
        request.setAttribute("wl_commodity_ByIdList",wl);
        return "forward:wl_category_getAllUpdate";
    }
    /**
     * 获取json数据  通过ajax方式返回前台
     * @return
     */
    @RequestMapping("wl_commodity_Ajax")
    @ResponseBody
    public String wl_commodity_Ajax(){
        return jsonData;
    }
    @RequestMapping("wl_commodity_Update")
    public String wl_commodity_Update(Wl_commodity wl_commodity,String shux[],String shuxz[],String[] channelcheck,HttpServletRequest request){
        String property="";
        for (int i=0;i<shux.length;i++){
            if(property.equals("")){
                property =property+ '"' + shux[i] + '"' + ":[\"" + shuxz[i] + "\"]";
            }else if(!shux[i].equals("") && shux[i]!=null){
                property =property+ ","+'"' + shux[i] + '"' + ":[\"" + shuxz[i] + "\"]";
            }
        }
        String newproperty = "{"+property+"}";
        wl_commodity.setProperty(newproperty);

        String [] image = request.getParameterValues("image");
        String [] lbimg = request.getParameterValues("imagelb");
        String images="";
        if(image!=null){
            for(int i=0;i<image.length;i++){
                //img是我自己在项目中添加的一个文件夹
                images+="img/"+image[i]+",/";
            }
        }
        if(lbimg!=null){
            for(int i=0;i<lbimg.length;i++){
                images+=lbimg[i]+"/";
            }
        }
        images=images.substring(0,images.lastIndexOf(",/"));

        wl_commodity.setImages(images);

        //遍历渠道存入
        String newchannelcheck="";
        for(int i=0;i<channelcheck.length;i++){
            newchannelcheck = newchannelcheck+channelcheck[i];
        }
        wl_commodity.setChannel(newchannelcheck);
        int result = wl_commodityService.updateByPrimaryKeySelective(wl_commodity);
        if(result>0){
            return "redirect:wl_commodity_get";
        }else{
            return "500";
        }
    }

    /**
     * 查询商品名称为活动管理新增做下拉框
     * 跳转至活动管理的新增页面
     */
//    @RequestMapping("wl_commodity_getByCommodityNames")
//    public String getByCommodityNames(ModelMap modelMap){
//        List<Wl_commodity> list = wl_commodityService.getByCommodityNames();
//        modelMap.addAttribute("wl_commodity_getByCommodityNames",list);
//        return "wl_activity_commodity_Insert";
//    }

    /**
     * 查询商品名称为活动管理新增做下拉框
     * 跳转至活动管理的新增页面
     */
    @RequestMapping("wl_commodity_getByCommodityName")
    @ResponseBody
    public List<Wl_commodity> getByCommodityName(String id){
        List<Wl_commodity>list = wl_commodityService.getByCommodityName(id);
        return list;
    }
}
